package com.study.business.configure;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

@Configuration
public class AppWebSecurityConfigurer extends WebSecurityConfigurerAdapter {


    @Resource(name = "userService")
    private UserDetailsService userDetailsService;


    protected AppWebSecurityConfigurer() {
        super();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();
        http.formLogin().and().authorizeRequests()
                .antMatchers("/error", "/index.html", "/main.js", "/static", "/app/**", "/theme/**", "/core/**").permitAll()
                .anyRequest().authenticated();
//        http.authorizeRequests().antMatchers("/core/**").permitAll()
//                .and().formLogin().and().authorizeRequests().anyRequest().authenticated();
    }


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }


    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
